<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="../css/layui.css">
  <script src="../layui.js"></script>
  <script src="../js/jquery-3.5.1.js"></script>
  <title>Document</title>
</head>

<body>
  <div style="padding: 15px;">

    <div class="layui-form-item layui-form-text " style="padding-top: 12px;">
      <label class="layui-form-label">主诉：</label>
      <div class="layui-input-block">
        <textarea class="layui-textarea" id="bl_pillness"></textarea>
      </div>
    </div>
    <div class="layui-form-item layui-form-text " style="padding-top: 12px;">
      <label class="layui-form-label">既往病史及其他病史：</label>
      <div class="layui-input-block">
        <textarea class="layui-textarea" id="bl_fillness"></textarea>
      </div>
    </div>
    <div class="layui-form-item layui-form-text " style="padding-top: 12px;">
      <label class="layui-form-label">过敏史</label>
      <div class="layui-input-block">
        <textarea class="layui-textarea" id="bl_allergy"></textarea>
      </div>
    </div>
    <div class="layui-form-item layui-form-text " style="padding-top: 12px;">
      <label class="layui-form-label">望闻切诊</label>
      <div class="layui-input-block">
        <textarea class="layui-textarea" id="bl_symptom"></textarea>
      </div>
    </div>
    <div class="layui-form-item layui-form-text " style="padding-top: 12px;">
      <label class="layui-form-label">辅助检查</label>
      <div class="layui-input-block">
        <textarea   class="layui-textarea" id="bl_assist"></textarea>
      </div>
    </div>
    <div class="layui-form-item layui-form-text " style="padding-top: 12px;">
      <label class="layui-form-label">诊断</label>
      <div class="layui-input-block">
        <textarea class="layui-textarea" id="bl_diag"></textarea>
      </div>
    </div>
    <div class="layui-form-item layui-form-text " style="padding-top: 12px;">
      <label class="layui-form-label">治疗意见</label>
      <div class="layui-input-block">
        <textarea class="layui-textarea" id="bl_suggestion"></textarea>
      </div>
    </div>
    <div class="layui-form-item layui-form-text " style="padding-top: 12px;">
      <label class="layui-form-label">病情变化及处置</label>
      <div class="layui-input-block">
        <textarea class="layui-textarea" id="bl_change"></textarea>
      </div>
    </div>

    <blockquote class="layui-elem-quote" style="margin-top: 10px;">
      处方
    </blockquote>
    <div style="text-align:center; padding-bottom: 12px;padding-top: 16;">
      <span style=" background-color:#5FB878;
        color: #fff; height: 35px; padding: 5px; list-style: 25;
         text-align:center; font-size: 14px;">处方单</span>
    </div>

    <hr>
    <div id="" style="background-color: #FAFAFA;">
      <div class="layui-row" style="padding: 0 2em; line-height: 25px;">
        <div class="layui-col-xs2" style="margin: 0; padding: 3px  1em;">
          <span style="padding: 3px;">药品名</span>
        </div>
        <div class="layui-col-xs2" style="margin: 0; padding: 3px  1em;">
          <span style="padding: 3px; ">药品规格</span>
        </div>
        <div class="layui-col-xs2" style="margin: 0; padding: 3px  1em;">
          <span style="padding: 3px;padding-left: 22px; ">数量</span>
        </div>
        <div class="layui-col-xs5" style="margin: 0; padding: 3px  1em;">
          <span style="padding: 3px; ">用法</span>
        </div>
      </div>

    </div>
    <hr style="margin: 1px;">
    <div id="yp">
      <div id="" style="background-color: #FAFAFA;">
        <div class="layui-row" style="padding: 0 2em; line-height: 25px;">
          <div class="layui-col-xs2" style="margin: 0; padding: 3px  1em;">
          </div>
          <div class="layui-col-xs2" style="margin: 0; padding: 3px  1em;">
          </div>
          <div class="layui-col-xs2" style="margin: 0; padding: 3px  1em;">
          </div>
          <div class="layui-col-xs5" style="margin: 0; padding: 3px  1em;">
          </div>
          </div>
        </div>

      </div>

      <div id="addyp" style="background-color: #FAFAFA; display: none;">
          <div class="layui-row" style="padding: 0 2em; line-height: 25px;">
            <div class="layui-col-xs2" style="margin: 0; padding: 3px  1em;">
              <span style="padding: 3px; ">拉米夫定</span>
            </div>
            <div class="layui-col-xs2" style="margin: 0; padding: 3px  1em;">
              <span style="padding: 3px; ">0.1g*7s*2板</span>
            </div>
            <div class="layui-col-xs2" style="margin: 0; padding: 3px  1em;">
              <div>
                <input type="text" name="" id="" style="width: 30px;margin-left: 22px;text-align: center;">
              </div>
            </div>
            <div class="layui-col-xs5" style="margin: 0; padding: 3px  1em;">
              <input type="text" style="width: 300px;">
            </div>
            <div class="layui-col-xs1" style="margin: 0; padding: 3px  1em;">
              <a class="delete" style=" padding: 3px; font-size: 12px; border-radius: 2px; background-color: #1E9FFF; color: white;cursor:pointer">删除</a>
            </div>
          </div>
      </div>
    </div>


        <hr>
        <div style="text-align: center;">
          <button class="layui-btn layui-btn-normal layui-btn-xs" id="save">保存处方单</button>
        </div>
        <hr>

  <div class="demo">
    <div class="layui-inline">
      <input class="layui-input" name="id" id="id" placeholder="药品代码" >
    </div>
    <button class="layui-btn" data-type="reload">搜索</button>
  </div>
        <!-- // 表格处理 -->

        <table class="layui-hide" id="test" lay-filter="test"></table>

  
        <script type="text/html" id="barDemo">
          <a class="layui-btn layui-btn-normal layui-btn-xs" id="add_yp" lay-event="del">添加药品</a>
        </script>

        <hr>
        <hr>


      <!-- 主体 -->

        <script>
          var bl_id = getUrlParam('bl_id');
          function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
          }
          var i=0;
          layui.use('table', function () {
            var table = layui.table;

            table.render({
              elem: '#test',
              id: 'allDrugTable',
              url: '/case/selectDrug',
              title: '用户数据表',
              cols: [
                [ //表头
                  {
                    field: 'yp_id',
                    title: '药品代码',
                    width: 110,
                    sort: true,
                    fixed: 'left'
                  }, {
                    field: 'yp_name',
                    title: '药品名称',
                    width: 120
                  }, {
                    field: 'yp_standard',
                    title: '规格',
                    width: 120
                  }, {
                    field: 'yp_form',
                    title: '剂型',
                    width: 80,
                    sort: true,
                    totalRow: true
                  }, {
                    field: 'yp_company',
                    title: '生产厂家',
                    width: 200,
                    sort: true
                  }, {
                    field: 'yp_uprice',
                    title: '价格',
                    width: 100,
                    sort: true,
                  }, {
                    fixed: 'right',
                    width: 80,
                    align: 'center',
                    toolbar: '#barDemo'
                  }
                ]
              ],
              page: true
              ,parseData: function (res) {
                return {
                  'code' : 0,
                  'msg' : '',
                  'count': res.length,
                  'data':res
                }
              }
            });

            var $ = layui.$, active = {
              reload:function (){
                var id = $('#id');
                var index = layer.msg('查询中，请稍候...',{icon:16,time:false,shade:0});
                setTimeout(function (){
                  table.reload('allDrugTable',{
                    page:{
                      curr:1
                    }
                    ,where:{
                      id:id.val()
                    }
                  });
                  layer.close(index);
                },800);
              }
            };
            $('.demo .layui-btn').on('click',function (){
              var type = $(this).data('type');
              active[type] ? active[type].call(this) : '';
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) {
              var data = obj.data;
              i++;
              var idname = "addyp"+i;
              var newdata = $("#addyp").clone();
              $(newdata).attr('id',idname);
              $(newdata).css('display',"block");
              // console.log(obj.data.yp_company);

              if (obj.event === 'del') {
                var a = $("#yp span").length;
                console.log(a);
                // console.log("xiugai"+$("$('boay').find(idname) span"));
                $("#yp").append(newdata);
                $("#yp span").eq(a).text(obj.data.yp_name);
                $("#yp span").eq(a+1).text(obj.data.yp_standard);
              } else if (obj.event === 'edit') {
                layer.prompt({
                  formType: 2,
                  value: data.email
                }, function (value, index) {
                  obj.update({
                    email: value
                  });
                  layer.close(index);
                });
              }
            });
          });
          $(document).on("click","#save",function(){
            var case1 = {
              "bl_pillness" : $("#bl_pillness").val(),
              "bl_fillness" : $("#bl_fillness").val(),
              "bl_allergy" : $("#bl_allergy").val(),
              "bl_symptom" : $("#bl_symptom").val(),
              "bl_assist" : $("#bl_assist").val(),
              "bl_diag" : $("#bl_diag").val(),
              "bl_suggestion" : $("#bl_suggestion").val(),
              "bl_change" : $("#bl_change").val()
            };
            $.ajax({
              url: '/case/update?bl_id='+bl_id,
              data: JSON.stringify(case1),
              contentType: 'application/json;charset=UTF-8',
              method: 'POST',
              success: function (){

              }
            });
            var prescription = [];
            var a = $("#yp span").length;
            for (let j = 0; j < a; j= j+2) {
              var json = {
                "yp_name": $("#yp span").eq(j).text(),
                "yp_standard": $("#yp span").eq(j+1).text(),
                "yz_amount": $("#yp input").eq(j).val(),
                "yz_dosage": $("#yp input").eq(j+1).val()
              };
              prescription.push(json);
            }
            $.ajax({
              url: '/case/insertPrescription?bl_id='+bl_id,
              data: JSON.stringify(prescription),
              contentType: 'application/json;charset=UTF-8',
              method: 'POST',
              success: function (){
                alert("成功");
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
                parent.location.reload();
              }
            });
          });
        </script>

        <!--表格处理 -->



<!-- 处方单操作 -->
<script>
  $(document).on('mouseenter',"i",function(){
    $(this).css('color', "orange");
  });
  $(document).on('mouseleave',"i",function(){
    $(this).css('color', "black");   
  });
  



  $(document).on('click',".delete",function(){
    $(this).parent().parent().remove();
  });
  // $(".delete").on('click',function(){
  //   $(this).parent().parent().remove();
    
  //   console.log($(this).parent().parent());
  // });
</script>
<!-- 处方单操作 end -->
</body>

</html>